<template>
  <div>
    <!-- <router-view /> -->
    <router-view v-slot="{ Component }">
      <transition>
        <keep-alive :exclude="['detail', 'register', 'addressedit', 'orderDetail']">
          <component :is="Component" />
        </keep-alive>
      </transition>
    </router-view>

    <div id="nav">
      <router-link class="tab-bar-itme" to="/">
        <div class="icon"><i class="iconfont icon-shouye"></i></div>
        <div>首页</div>
      </router-link>
      <router-link class="tab-bar-itme" to="/category">
        <div class="icon"><i class="iconfont icon-fenlei"></i></div>
        <div>分类</div>
      </router-link>
      <router-link class="tab-bar-itme" to="/shopcart">
        <div class="icon">
          <van-badge :content="getCartsCount" max="99">
            <i class="iconfont icon-gouwuche"></i>
          </van-badge>
        </div>
        <div>购物车</div>
      </router-link>
      <router-link class="tab-bar-itme" to="/profile">
        <div class="icon"><i class="iconfont icon-icon-test"></i></div>
        <div>我的</div>
      </router-link>
    </div>
    <Loading v-show="loading" />
  </div>
</template>

<script>
import { mapState, mapGetters } from "vuex";
import Loading from "components/common/loading/Loading";
import store from "./store";

export default {
  computed: {
    ...mapState(["loading"]),
    ...mapGetters(["getCartsCount"]),
  },
  components: {
    Loading,
  },
  created() {
    console.log(store);
    // 防止页面刷新 vuex数据丢失 https://segmentfault.com/a/1190000038950555
    if (window.localStorage.getItem("refreshData")) {
      store.replaceState(
        Object.assign(
          {},
          store.state,
          JSON.parse(window.localStorage.getItem("refreshData"))
        )
      );
      window.localStorage.removeItem("refreshData");
    }
    window.addEventListener("beforeunload", () => {
      window.localStorage.setItem("refreshData", JSON.stringify(store.state));
    });
  },
};
</script>

<style lang="scss">
@import "assets/css/base.css";
@import "assets/iconFont/iconfont.css";
#app {
  font-family: Avenir, Helvetica, Arial, sans-serif;
  -webkit-font-smoothing: antialiased;
  -moz-osx-font-smoothing: grayscale;
  text-align: center;
  color: #2c3e50;
}

#nav {
  position: fixed;
  left: 0;
  right: 0;
  bottom: 0;
  z-index: 10;
  box-shadow: 0 -2px 1px rgba(100, 100, 100, 0.1);
  display: flex;
  justify-content: space-around;
  background-color: #f6f6f6;
  a {
    color: var(--color-text);
    &.router-link-exact-active {
      color: var(--color-hight-text);
    }
  }
  .tab-bar-itme {
    height: 56px;
    text-align: center;
    font-size: var(--font-size);
  }
  .tab-bar-itme .icon {
    display: inline-block;
    width: 26px;
    height: 26px;
    vertical-align: middle;
    margin-top: 6px;
  }
}
</style>
